<?php
/* --------------------------------------------------------------
   GMEbay.php 2011-07-01 gambio
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2011 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/
	/*
	* class to include ebay articles 	
	*/
	class gmEbay {

		var $gm_ebay_name;

		var $gm_ebay_site_id;

		var $gm_ebay_use;

		var $gm_ebay_count;

		var $gm_ebay_articles = array();

		var $gm_ebay_path;
		
		var $gm_ebay_time;

		var $gm_ebay_server = "http://rss.api.ebay.com";


		/*
		* class to include ebay articles 	
		*/
		function gmEbay($gm_ebay_conf) {

			$this->gm_ebay_name			= $gm_ebay_conf['GM_EBAY_NAME'];

			$this->gm_ebay_site_id		= $gm_ebay_conf['GM_EBAY_SITE_ID'];

			$this->gm_ebay_use			= $gm_ebay_conf['GM_EBAY_USE'];

			$this->gm_ebay_count		= $gm_ebay_conf['GM_EBAY_COUNT'];

			$this->gm_ebay_time			= time();
			
			if($gm_ebay_conf['GM_EBAY_TYP'] == 0) {
				$this->gm_ebay_path			=  "/ws/rssapi?FeedName=SearchResults&siteId=" . $this->gm_ebay_site_id  . "&output=RSS20&sass=" . $this->gm_ebay_name  . "";
			} else {
				$this->gm_ebay_path			=  "/ws/rssapi?FeedName=StoreItems&siteId=" . $this->gm_ebay_site_id  . "&output=RSS20&storeId=" . $this->gm_ebay_name  . "";		
			}			

			return;
		}

		
		/*
			-> function to set the ebay articles
		*/
		function gmEbay_set() {			
			
			if ($this->gm_ebay_use == 'curl') {

				$data = $this->gmEbay_curl();

			} else {

				$data = $this->gmEbay_fsockopen();
			}
			
			// -> get section with plain items
			$data = $this->gmEbay_cut_rss($data, '<item>', '</channel>', 6);
			
			// -> get plain items
			$data_array = explode('<item>', $data);

				foreach ($data_array as $item) {

					$data = $this->gmEbay_cut_rss($item, '<title>', '</description>', 0);
					$data = $this->gmEbay_clean_rss($data);
					$this->gm_ebay_articles[] = explode("\n", $data);
				}

			$this->gm_ebay_articles = $this->multi_unique($this->gm_ebay_articles);

			return;
		}

		
		function multi_unique($p_array)
		{
			foreach($p_array AS $t_key => $t_value)
			{
				$t_array[$t_key] = serialize($t_value);
			}

			$t_unique_array = array_unique($t_array);
			
			foreach($t_unique_array AS $t_key => $t_value)
			{
				$t_final_array[$t_key] = unserialize($t_value);
			}

			return ($t_final_array);
		}


		/*
			-> function to get the ebay output
		*/
		function gmEbay_get($gm_ebay_start) {		
			$page =  $gm_ebay_start / $this->gm_ebay_count + 1;
			$pages = count($this->gm_ebay_articles) / $this->gm_ebay_count;				

			if(empty($gm_ebay_start)) {
				$gm_ebay_start=0;
			}

			for($i = $gm_ebay_start; $i < $this->gm_ebay_count + $gm_ebay_start; $i++) {
				if(!empty($this->gm_ebay_articles[$i])) {
					$ebay_auction[] = $this->gmEbay_getItem($this->gm_ebay_articles[$i]);
				}
			}

			$smarty = new Smarty;
			$smarty->assign('language', $_SESSION['language']);
			if(!empty($this->gm_ebay_articles[0][0])) {
				$smarty->assign('GM_EBAY_NAVIGATION',$this->gmEbay_navigate($page, $pages));
				$smarty->assign('BUTTON_BUY_NOW', xtc_image_button('button_view.gif', 'ebay'));
				$smarty->assign('GM_EBAY_AUCTIONS', $ebay_auction);
			} else {
				$smarty->assign('error', EBAY_EMPTY);
			}				
			$smarty->display(CURRENT_TEMPLATE.'/module/gm_ebay.html');
			
			return;
		}


		/*
			-> function to format description
		*/
		function gmEbay_getItem($item) {

				$ebay_auction['title'] = htmlspecialchars(utf8_decode($item[0]));

				$ebay_auction['link'] = $item[1];

				$description = explode("<br />" ,$item[2]);

				$ebay_auction['price'] =  $this->gmEbay_cut_rss($description[0], '<strong>', '</strong>', 8);

				if (preg_match("/Gebot/", $description[0])) {
					$bids =  $this->gmEbay_cut_rss($description[0], '(', ')', 1);
					$bids = explode(' ', $bids);
					$ebay_auction['bids'] =  $bids[0];
				}
			
				$ebay_auction['image'] = $this->gmEbay_cut_rss($description[0], '<a', '></a>', 0) . ' alt="' . $ebay_auction['title'] . '" title="' . $ebay_auction['title'] . '"/></a>';
				$ebay_auction['end_time'] = utf8_decode(trim($this->gmEbay_get_date($description[1])));

			return $ebay_auction;
		}


		/*
			-> function to get the substring
		*/
		function gmEbay_get_date($end_date) {
			$end_date	= strstr($end_date, ':');
			$end_date	= explode(' ' ,strstr($end_date, ':'));
			$array_date = explode('-' ,$end_date[2]);
			$array_time = explode(':' ,$end_date[3]);

			
			return $array_date[1] . '. ' . $array_date[0] . ' ' . $array_date[2] . ' ' . sprintf('%02d', $array_time[0]) . ':' . $array_time[1];
		}

		
		/*
			-> function to get the substring
		*/
		function gmEbay_cut_rss($haystack, $start_needle, $end_needle, $count) {
			$start = strpos($haystack, $start_needle);
			$end = strpos($haystack, $end_needle);
			$haystack = substr($haystack, $start+$count, $end-$start-$count);
			return $haystack;
		}


		/*
			-> function to get of rss-elements
		*/
		function gmEbay_clean_rss($haystack) {
			$array = array(
						'<![CDATA[',
						']]>',
						'<title>',
						'</title>',
						'<link>',
						'</link>',
						'<description>',
						'<description>'
					);

			foreach($array as $item) {
				$haystack = str_replace($item, '', $haystack);
			}

			return $haystack;
		}


		/*
			-> function to get ebay rss file - using curl
		*/
		function gmEbay_curl() {

			$ch = curl_init();

			curl_setopt($ch, CURLOPT_URL, $this->gm_ebay_server . $this->gm_ebay_path);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

			$data = curl_exec($ch);

			curl_close($ch);

			return $data;
		}


		/*
			-> function to get ebay rss file - using fsockopen
		*/
		function gmEbay_fsockopen() {

			$sock = fsockopen($this->gm_ebay_server, 80, $errno, $errstr, 5);
			fputs($sock, "GET " . $this->gm_ebay_path . " HTTP/1.1\r\n");
			fputs($sock, "Host: " . $this->gm_ebay_server . "\r\n");
			fputs($sock, "Connection: close\r\n\r\n");

			while(!feof($sock)) {
				$data .= fgets($sock, 4096);
			}
			
			fclose($sock);
			
			return $data;
		}


		/*
			-> function to navigate
		*/
		function gmEbay_navigate($page, $pages) {

			$SEF_parameter = '';
			if (GROUP_CHECK == 'true') {
				$group_check = " AND group_ids LIKE '%c_".$_SESSION['customers_status']['customers_status_id']."_group%'";
			}
			$gm_query = xtc_db_query("
									SELECT
										content_title
									FROM " . 
										TABLE_CONTENT_MANAGER . "
									WHERE 
										content_group = '98'
									AND
										languages_id='".(int) $_SESSION['languages_id'] . "'
									" . $group_check . "" 
									); 

			$content_data = xtc_db_fetch_array($gm_query);
			if (SEARCH_ENGINE_FRIENDLY_URLS == 'true') {
				$SEF_parameter = '&content='.xtc_cleanName($content_data['content_title']);
			}
			
			if($pages > 1) {
				if($page != 1) {

					$gm_ebay_nav .= '<a href="' . xtc_href_link(FILENAME_CONTENT, 'coID=98' . $SEF_parameter . '&gm_ebay_start=' .  (($page * $this->gm_ebay_count) - $this->gm_ebay_count*2)). '">' . GM_EBAY_BACK . '</a> | ';

				}

				for($j = 1; $j < round($pages)+1; $j++) {

					if($page == $j) {

						$gm_ebay_nav .= '<strong><a href="' . xtc_href_link(FILENAME_CONTENT, 'coID=98' . $SEF_parameter . '&gm_ebay_start=' . ($j - 1) * $this->gm_ebay_count) . '">' . $j . '</a></strong> | ';
					
					} else {

						$gm_ebay_nav .= '<a href="' . xtc_href_link(FILENAME_CONTENT, 'coID=98' . $SEF_parameter . '&gm_ebay_start=' . ($j - 1) * $this->gm_ebay_count) . '">' . $j . '</a> | ';
					}

				}

				if($page != round($pages)) {

					$gm_ebay_nav .= '<a href="' . xtc_href_link(FILENAME_CONTENT, 'coID=98' . $SEF_parameter . '&gm_ebay_start=' .  $page * $this->gm_ebay_count) . '">' . GM_EBAY_FORWARD . '</a>';

				} else {
					$gm_ebay_nav = substr($gm_ebay_nav, 0, strlen($gm_ebay_nav)-2);
				}

				return $gm_ebay_nav;
			}
		}


		/*
			-> function to admin
		*/
		function gmEbay_admin() 
		{
			if(!empty($_POST['gm_submit'])) 
			{		
				unset($_POST['gm_submit']);		
				unset($_POST['GM_EBAY_HIDDEN_TYP']);

				if(empty($_POST['GM_EBAY_COUNT'])) 
				{
					$_POST['GM_EBAY_COUNT'] = 1;
				}

				foreach($_POST as $key => $value) 
				{
					gm_set_conf($key, $value);
				}
			}
			
			if($_POST['GM_EBAY_ACTIVE'] == 'false')
			{
				xtc_db_query("
								UPDATE
									gm_boxes
								SET
									box_status = '0'
								WHERE
									box_name = 'gm_ebay'												
				");

			}
			else if($_POST['GM_EBAY_ACTIVE'] == 'true')
			{
				xtc_db_query("
								UPDATE
									gm_boxes
								SET
									box_status = '1'
								WHERE
									box_name = 'gm_ebay'												
				");
			}
			
			$gm_ebay_conf = gm_get_conf(
										array(
												'GM_EBAY_ACTIVE', 
												'GM_EBAY_NAME', 
												'GM_EBAY_COUNT', 
												'GM_EBAY_USE', 
												'GM_EBAY_TYP', 
												'GM_EBAY_SITE_ID'
												)
										);

			return $gm_ebay_conf;
		}
	}

?>